<?php

namespace app\admin\controller\finance;

use app\common\controller\Backend;
use think\Db;

/**
 * 商家钱包流水
 *
 * @icon fa fa-circle-o
 */
class Walletlog extends Backend {

    /**
     * BusinessWalletLog模型对象
     * @var \app\admin\model\BusinessWalletLog
     */
    protected $model = null;

    public function _initialize() {
        parent::_initialize();
        $this->model = model('BusinessWalletLog');
        $this->view->assign("changeTypeList", $this->model->getChangeTypeList());
        $this->view->assign("changeMattersList", $this->model->getChangeMattersList());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index() {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $otherwhere[] = ['b.admin_id', '=', $this->iAdminId];
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(null, null, $otherwhere);
            $total = $this->model
                    ->alias('bwl')
                    ->join('fa_business_wallet bw', 'bwl.wallet_id = bw.wallet_id')
                    ->join('fa_business b', 'b.business_id = bw.business_id')
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->alias('bwl')
                    ->field('bwl.*,b.business_title')
                    ->join('fa_business_wallet bw', 'bwl.wallet_id = bw.wallet_id')
                    ->join('fa_business b', 'b.business_id = bw.business_id')
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as &$row) {
                if ($row['change_type'] == 0) {
                    $row['change_amount'] = '+' . $row['change_amount'];
                } else {
                    $row['change_amount'] = '-' . $row['change_amount'];
                }

                if ($row['change_matters'] < 3) {
                    $row['task_id'] = Db::name('task')->where(['task_id' => $row['task_id']])->value('task_no');
                }else{
                    $row['task_id'] = '-';
                }
            }
            $money = Db::table('fa_business')
                    ->alias('b')
                    ->join('fa_business_wallet bw', 'b.business_id = bw.business_id')
                    ->where(['b.admin_id' => $this->iAdminId])
                    ->value('bw.cash_amount');
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list, "extend" => ['money' => $money]);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function add() {
        return $this->view->fetch();
    }

    public function recharge() {
        $amount = input('amount', 0);

        $out_trade_no = 'BLS' . time() . mt_rand(1000, 9999);
        $data['admin_id'] = $this->iAdminId;
        $data['business_id'] = Db::name('business')->where(['admin_id' => $this->iAdminId])->value('business_id');
        $data['out_trade_no'] = $out_trade_no;
        $data['amount'] = $amount * 100;
        $data['createtime'] = time();

        $recharge = model('Recharge');
        $recharge->data($data);
        $recharge->save();

        $res['out_trade_no'] = $out_trade_no;
        $res['amount'] = $amount * 100;
        $url = url('wxpay/pay', $res);
        // 前往支付
        $this->redirect($url);
    }

}
